﻿using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Infrastructure.Search;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using System;
using System.Web;
using System.Web.Mvc;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    public class MessageController : SharpController
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 收件箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessagePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 自动完成分页数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public ActionResult AutocompletePaged(string keyword, int pageNo = 1)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                PagingFeature pf = new PagingFeature(UIEngine.None)
                {
                    PageNo = pageNo,
                    PageSize = 15,
                    RequestString = "userName=" + HttpUtility.UrlEncode(keyword)
                };
                var pageInfo = proxy.Channel.GetUsersPaged(pf);

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 发件箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetOutboxPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageOutboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 草稿箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDraftboxPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDraftboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 垃圾箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDustbinPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDustbinPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        [AutomaticTemplate]
        public ActionResult Details(int id)
        {
            MessageDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View(dto.To<MessageModel>());
        }

        public ActionResult Create()
        {
            var vm = new MessageModel();

            return View("Create", vm);
        }

        
        public ActionResult Send(MessageModel message)
        {
            MessageDTO dto = message.To<MessageDTO>();

            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SendMessage(dto);

                return this.JsonNet(res);
            }
        }
        
        public ActionResult Draft(MessageModel message)
        {
            MessageDTO dto = message.To<MessageDTO>();

            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DraftMessage(dto);

                return this.JsonNet(res);
            }
        }
        
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                res = proxy.Channel.DeleteMessages(ids);
            }

            return this.JsonNet(res);
        }
    }
}